package com.winit.openapi.ups.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.winit.common.spi.context.CommandContext;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.erp.controller.DeliveryWayController;
import com.winit.openapi.model.ResponseMsg;
import com.winit.ups.spi.systemrule.TransportModeService;
import com.winit.ups.spi.systemrule.command.TransportModeCommand;
import com.winit.ups.spi.systemrule.vo.TransportMode;

/**
 * 查询运输方式
 * 
 * @author adminchenliu
 */
@Controller
@RequestMapping(value = "/ups/transportMode")
public class TransportModeController extends BaseController {

    private Logger               logger = LoggerFactory.getLogger(DeliveryWayController.class);

    @Resource
    private TransportModeService transportModeService;

    @RequestMapping(value = "/queryTransportMode")
    public String queryDelivery(@ModelAttribute ResponseMsg responseMsg) {
        logger.info("开始查询运输方式");
        TransportModeCommand command = new TransportModeCommand();
        TransportMode transportMode = new TransportMode();
        command.setTransportMode(transportMode);
        List<TransportMode> tList = transportModeService.getTransportModeList(command);
        if (tList.size() < 0) {
            logger.debug("没有查询到运输方式");
            return ERROR;
        } else {
            CommandContext ctx = CommandContext.getContext();
            String language = ctx.getLanguageCountry();
            List<TransportMode> listTransportMode = new ArrayList<TransportMode>();
            for (TransportMode tm : tList) {
                TransportMode stransportMode = new TransportMode();
                stransportMode.setTransportModeCode(tm.getTransportModeCode());
                if ("en_US".equals(language)) {
                    stransportMode.seteName(tm.geteName());
                } else {
                    stransportMode.setName(tm.getName());
                }
                listTransportMode.add(stransportMode);
            }
            logger.info("查询运输方式有" + listTransportMode.size() + "条");
            responseMsg.setData(listTransportMode);
        }
        return SUCCESS;
    }
}
